package com.zabra.zl.ui.allocation_plan.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.MoneyRuleBean;
import com.zabra.zl.data.MultipleRuleBean;
import com.zabra.zl.ui.allocation_plan.contract.AllocationPlanContract;
import com.zabra.zl.ui.allocation_plan.model.AllocationPlanModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AllocationPlanPresenter extends BasePresenter<AllocationPlanContract.View> implements AllocationPlanContract.Presenter {
    private AllocationPlanContract.Model model;

    public AllocationPlanPresenter() {
        model = new AllocationPlanModel();
    }

    @Override
    public void getMoneyRuleData(int bpType) {
        if (!isViewAttached()) {
            return;
        }
        model.getMoneyRuleData(bpType).enqueue(new Callback<MoneyRuleBean>() {
            @Override
            public void onResponse(Call<MoneyRuleBean> call, Response<MoneyRuleBean> response) {
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<MoneyRuleBean> call, Throwable t) {
                mView.onError(t);
            }
        });
    }

    @Override
    public void getMultipleRuleData(int bpType) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getMultipleRuleData(bpType).enqueue(new Callback<MultipleRuleBean>() {
            @Override
            public void onResponse(Call<MultipleRuleBean> call, Response<MultipleRuleBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token", response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<MultipleRuleBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
